package service

import (
	"context"
	"fmt"
	__ "hospital-srv/basic/proto"
	"hospital-srv/handler/dao"
)

// 一级评论添加
func (s *Serve) ProductCommentsAdd(_ context.Context, in *__.ProductCommentsAddReq) (*__.ProductCommentsAddResp, error) {

	if in.ProductId <= 0 || in.UserId <= 0 || in.Content == "" || in.Rating <= 0 {
		return nil, fmt.Errorf("参数错误")
	}

	create, err := dao.ProductCommentAdd(in)
	if err != nil {
		return nil, fmt.Errorf(err.Error())
	}

	return &__.ProductCommentsAddResp{
		Id: int32(create.ProductId), //商品id
	}, nil
}

// 二级评论添加
func (s *Serve) CommentReplyAdd(_ context.Context, in *__.CommentReplyAddReq) (*__.CommentReplyAddResp, error) {

	create, err := dao.CommentReplyAdd(in)
	if err != nil {
		return nil, fmt.Errorf(err.Error())
	}

	return &__.CommentReplyAddResp{
		Id: int32(create.CommentId), //一级评论id
	}, nil
}

// 商品id查询评论
func (s *Serve) GetCommentsByProductId(_ context.Context, in *__.GetCommentsByProductIdReq) (*__.GetCommentsByProductIdResp, error) {

	if in.ProductId <= 0 {
		return nil, fmt.Errorf("参数错误")
	}

	comments, err := dao.GetCommentsByProductId(in)
	if err != nil {
		return nil, fmt.Errorf(err.Error())
	}

	return &__.GetCommentsByProductIdResp{
		Comments: comments, //评论列表
	}, nil
}
